<template>
    <div>
        <table class="table_detail" style="width: 100%;">
                <tr> <td colspan="5" class="headColor"> <span>附件下载</span></td></tr>
                <tr class="posR">
                    
                        <td class="normalC showText" style="width: 100%;">
                            <template v-for="(v,i) in fileList">
                                 <span>{{v.name}}</span>
                            </template>
                            <br><br><br><br>
                        </td>
                        <div class="posA">
                            <el-button @click="downloadFile" type="primary">下载附件</el-button>
                        </div>                    
                </tr>                                                                                                                                                                             
        </table>           

    </div>
</template>

<script>
    export default {
        props:{
            // cText:'',
            cData:'',
            // cCol:'',
        },
        watch: {
            cData(newValue, oldValue) {
               
                // this.urlList = newValue
                this.fileList = [];
                newValue.forEach(element => {
                    let data = {
                        name: element.annexName,
                        url: element.annexUrl,
                    } 
                    this.fileList.push(data);     
                });
            }
        },        
        data() {
            return {
                fileList:[],        
            }
        },
        methods:{
            downloadFile(){
                
                 let subjectId = this.$route.query.id;
            
                 this.get('/api/subjectmaininfo/getAnnexNum/'+subjectId+'/0').then(res=>{
                     if(res.code===0 && res.data>0){
                            // console.log(this.cData);
                            // console.log(this.cData[0].subjectId);
                            // let subjectId = this.cData[0].subjectId;
                            axios({
                                method: 'get',
                                responseType: "arraybuffer",
                                url: `/api/subjectmaininfo/downloadAttached/`+subjectId+`/0`,
                            }).then(function (response) {
                                console.log(response)
                                let blob = new Blob([response.data]);
                                let url = window.URL.createObjectURL(blob);
                                let link = document.createElement('a')
                                link.style.display = 'none'
                                link.href = url
                                link.setAttribute('download', '标的附件' + '.zip')

                                document.body.appendChild(link)
                                link.click()
                                //释放URL对象所占资源
                                window.URL.revokeObjectURL(url)
                                //用完即删
                                document.body.removeChild(link)
                            })
                                .catch(function (error) {
                                    console.log(error);
                                });
                     }else{
                         alert("没有上传附件")
                     }
                 });
               
            }
        }
    }
</script>

<style scoped>
    .normalC{ background-color: #ffffff !important;}
    .showC{ background-color: #f5f5f5 !important;}
    .table_detail td:nth-child(odd) {
        width: 240px;
        height: 40px;
        background-color: #f5f5f5;
        flex: none;
    }    
    .showText{    
        padding: 20px;
        line-height: 25px;
    }
    .posA{
        position: absolute;
        right: 20px;
        bottom: 20px
    }
    .el-button--primary{
        background-color: #00456c;
    }
</style>